﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;

namespace TravelSite.Api.Admin
{
    public partial class PackageDestinationOptionPage : BaseDataPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadCombo()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ApiVacationOptionRepository>())
            {
                ddlOptionId.DataSource = repo.FindNotInDest(PkgDestId);
                ddlOptionId.DataBind();
            }
        }

        public override void LoadData()
        {
            PackageDestination pd = null;
            TravelPackage tp = null;
            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                pd = repo.FindDestination(PkgDestId);
                if (pd != null)
                {
                    tp = repo.Find(pd.PackageId);

                    hlPackageName.Text = tp.PackageName;
                    hlPackageName.NavigateUrl = "~/Admin/PackageDestination.aspx?pid=" + tp.PackageId.ToString();

                    lblDestinationName.Text = pd.DestinationName;

                    grid.DataSource = repo.FindOptions(PkgDestId);
                    grid.DataBind();
                }
                else
                {
                    hlPackageName.Text = "NOT FOUND";
                    lblDestinationName.Text = "NOT FOUND";
                    btnAdd.Visible = false;
                }
            }
        }
        
        protected void upDetails_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["__EVENTTARGET"]))
                if (Request["__EVENTTARGET"] != upDetails.ClientID)
                    return;
            
            if (!string.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
            {
                hdId.Text = Request["__EVENTARGUMENT"];
                LoadCombo();

                if (ddlOptionId.Items.Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('No Option Available!');", true);
                    return;
                }

                if (hdId.Text != "-1")
                {
                    /*using (var repo = RepositoryFactory.ResolveRepository<ApiOptionChoiceRepository>())
                    {
                        lblDialogTitle.Text = "Edit Option";
                        OptionChoice item = repo.Find(Convert.ToInt32(hdId.Text));
                        txtChoiceDescription.Text = item.ChoiceDescription;
                        ddlOptionId.SelectedValue = item.OptionId.ToString();
                    }*/
                }
                else
                {
                    lblDialogTitle.Text = "Add Option";
                    if (ddlOptionId.Items.Count > 0)
                        ddlOptionId.SelectedIndex = 0;
                }
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowEdit", "$('#divDialog').dialog('open');", true);
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                PackageDestinationOption itm;

                if (hdId.Text == "-1")
                {
                    itm = new PackageDestinationOption();
                    AssignValues(itm);
                    repo.InsertOption(itm);
                }
                else
                {
                    /*itm = repo.Find(Convert.ToInt32(hdId.Text));
                    AssignValues(itm);
                    repo.Update(itm);*/
                }
            }

            LoadData();
            upMain.Update();
        }

        void AssignValues(PackageDestinationOption itm)
        {
            itm.OptionId = Convert.ToInt32(ddlOptionId.SelectedValue);
            itm.PkgDestId = PkgDestId;
        }

        protected void grid_RowCommand(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                switch (e.CommandName)
                {
                    case "delete":
                        repo.DeleteOption(PkgDestId, id);
                        break;
                }
            }
            LoadData();
        }

        protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                PackageDestinationOption item = (PackageDestinationOption)e.Row.DataItem;

                HyperLink hl = (HyperLink)e.Row.FindControl("hlOptionName");
                hl.NavigateUrl = ResolveClientUrl(string.Format("~/Admin/OptionChoice.aspx?pdid={0}&oid={1}", item.PkgDestId.ToString(), item.OptionId.ToString()));

                ImageButton ibtn = (ImageButton)e.Row.FindControl("lbtnDelete");
                ibtn.Visible = item.CanDelete;
            }
        }

        public int PkgDestId
        {
            get
            {
                int result = -1;
                if (!string.IsNullOrEmpty(Request.QueryString["pdid"]))
                    result = Convert.ToInt32(Request.QueryString["pdid"]);
                return result;
            }
        }
    }
}